package com.iflytek.edu.db.core.strategy;

import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.util.StringUtils;
import com.iflytek.edu.db.core.enums.StrategyType;

/**
 * 方法适配策略
 *
 * @author linkai
 * @date 2022/1/12
 */
public interface FunctionStrategy extends Strategy {

    default StrategyType strategyType() {
        return StrategyType.function;
    }

    default boolean isMethod(SQLObject obj) {
        return obj instanceof SQLMethodInvokeExpr;
    }

    default boolean checkName(SQLObject obj, String methodName) {
        return isMethod(obj)
                &&
                StringUtils.equalsIgnoreCase(
                        ((SQLMethodInvokeExpr) obj).getMethodName(),
                        methodName
                );
    }

    default void convertName(SQLObject obj, String methodName) {
        if (isMethod(obj)) {
            ((SQLMethodInvokeExpr) obj).setMethodName(methodName);
        }
    }

    default String getMethodName(SQLObject obj) {
        if (isMethod(obj)) {
            return ((SQLMethodInvokeExpr) obj).getMethodName();
        }
        return null;
    }

}
